package me.zhengjie.modules.biz.rest;

import cn.hutool.core.convert.Convert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.biz.domain.HazardousChemicals;
import me.zhengjie.modules.biz.repository.HazardousChemicalsRepository;
import me.zhengjie.modules.biz.service.HazardousChemicalsService;
import me.zhengjie.modules.biz.service.dto.HazardousChemicalsDto;
import me.zhengjie.modules.biz.service.dto.HazardousChemicalsQueryCriteria;
import me.zhengjie.modules.utils.BaseResult;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.ValidationUtil;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Admin
 * @website https://eladmin.vip
 * @date 2023-03-03
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "hazardous-chemicals管理")
@RequestMapping("/api/hazardousChemicals")
public class HazardousChemicalsController implements CommandLineRunner {

    private final HazardousChemicalsService hazardousChemicalsService;
    private final HazardousChemicalsRepository hazardousChemicalsRepository;


    @Log("导出器材数据")
    @ApiOperation("导出器材数据")
    @GetMapping(value = "/exportEquipment")
//    @PreAuthorize("@el.check('hazardousChemicals:list')")
    public void exportEquipment(HttpServletResponse response, HazardousChemicalsQueryCriteria criteria) throws IOException {
        hazardousChemicalsService.exportEquipment(hazardousChemicalsService.queryAll(criteria), response);
    }

    @Log("导出标准物质数据")
    @ApiOperation("导出标准物质数据")
    @GetMapping(value = "/exportMaterials")
    public void exportChemical(HttpServletResponse response, HazardousChemicalsQueryCriteria criteria) throws IOException {
        hazardousChemicalsService.exportMaterials(hazardousChemicalsService.queryAll(criteria), response);
    }

    @Log("导出化学试剂数据")
    @ApiOperation("导出化学试剂数据")
    @GetMapping(value = "/exportChemicals")
    public void exportEquipments(HttpServletResponse response, HazardousChemicalsQueryCriteria criteria) throws IOException {
        hazardousChemicalsService.exportChemicals(hazardousChemicalsService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询hazardous-chemicals")
    @ApiOperation("查询hazardous-chemicals")
    public ResponseEntity<Object> queryHazardousChemicals(HazardousChemicalsQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(hazardousChemicalsService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增hazardous-chemicals")
    @ApiOperation("新增hazardous-chemicals")
    public ResponseEntity<Object> createHazardousChemicals(@Validated @RequestBody HazardousChemicals resources) {
        HazardousChemicalsDto hazardousChemicalsDto = hazardousChemicalsService.create(resources, true);
        if (hazardousChemicalsDto.getName().equals("请勿重复入库同一物质！")) {
            return new ResponseEntity<>("请勿重复入库同一物质！", HttpStatus.BAD_REQUEST);
        } else {
            return new ResponseEntity<>("入库成功", HttpStatus.OK);
        }
    }

    @PutMapping
    @Log("修改hazardous-chemicals")
    @ApiOperation("修改hazardous-chemicals")
//    @PreAuthorize("@el.check('hazardousChemicals:edit')")
    public ResponseEntity<Object> updateHazardousChemicals(@Validated @RequestBody HazardousChemicals resources) {
        hazardousChemicalsService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除hazardous-chemicals")
    @ApiOperation("删除hazardous-chemicals")
//    @PreAuthorize("@el.check('hazardousChemicals:del')")
    public ResponseEntity<Object> deleteHazardousChemicals(@RequestBody Integer[] ids) {
        hazardousChemicalsService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("refrigeratorHazarddousChemicalsList")
    @ApiOperation("冰箱标准物质列表")
    public ResponseEntity<Object> refrigeratorHazarddousChemicalsList(Integer id) {
        return new ResponseEntity<>(hazardousChemicalsService.refrigeratorHazarddousChemicalsList(id), HttpStatus.OK);
    }

    @ApiOperation("汇总数据")
    @GetMapping(value = "/collect")
    public ResponseEntity<Object> collectHazardousChemicals(HazardousChemicalsQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(hazardousChemicalsService.collect(criteria, pageable), HttpStatus.OK);
    }

    @ApiOperation("打印")
    @AnonymousAccess
    @GetMapping(value = "/print")
    public ResponseEntity<Object> print(Integer id, HttpServletResponse response) {
        hazardousChemicalsService.printBarCode(id, response);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("申请列表分页")
    @GetMapping(value = "/applyListPage")
    public ResponseEntity<Object> applyList(HazardousChemicalsQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(hazardousChemicalsService.applyListPage(criteria, pageable), HttpStatus.OK);
    }

//    @ApiOperation("申请列表")
//    @AnonymousAccess
//    @GetMapping(value = "/applyList")
//    public ResponseEntity<Object> applyList(Integer type){
//        Map temp = new HashMap();
//        temp.put("data", hazardousChemicalsService.applyList(type));
//        return new ResponseEntity<>(temp ,HttpStatus.OK);
//
//    }

    @ApiOperation("申请列表")
    @AnonymousAccess
    @GetMapping(value = "/applyList")
    public ResponseEntity<Object> applyListTotal(Integer type) {
        Map temp = new HashMap();
        temp.put("data", hazardousChemicalsService.queryAllByRequest(type));
        return new ResponseEntity<>(temp, HttpStatus.OK);
    }

    @ApiOperation("销库")
    @AnonymousAccess
    @PostMapping(value = "/burning")
    public ResponseEntity<Object> burning(@RequestBody Map<String, Integer[]> ids) {
        hazardousChemicalsService.burning(ids.get("ids"));
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("删除")
    @AnonymousAccess
    @PostMapping(value = "/destroy")
    public ResponseEntity<Object> destroy(@RequestBody Map<String, Integer[]> ids) {
        hazardousChemicalsService.destroy(ids.get("ids"));
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/real")
//        hazardousChemicalsService.isPastDueH();
    public void isPastDue() {
        hazardousChemicalsService.isPastDueH();
    }

    @Override
    public void run(String... args) throws Exception {
//        isPastDue();
    }

    @ApiOperation("查询过期列表")
    @Log("查询过期列表")
    @AnonymousAccess
    @GetMapping(value = "/expireList")
    public ResponseEntity<Object> expireList(HazardousChemicalsQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(hazardousChemicalsService.expireList(criteria, pageable), HttpStatus.OK);
    }

//    @ApiOperation("销库")
//    @AnonymousAccess
//    @PostMapping(value = "/destroyByIds")
//    public ResponseEntity<Object> destroyByIds(@RequestBody Integer[] ids) {
//        hazardousChemicalsService.destroyByIds(ids);
//        return new ResponseEntity<>(HttpStatus.OK);
//    }

    @GetMapping(value = "/queryNumByCode")
    public ResponseEntity<Object> queryNumByCode(@RequestParam int id) {
        HazardousChemicalsDto hazardousChemicalsDto = hazardousChemicalsService.findById(id);

        return new ResponseEntity<>(hazardousChemicalsDto, HttpStatus.OK);
    }

    @GetMapping(value = "/queryNamesByType")
    public ResponseEntity<Object> queryNameByType(@RequestParam int type) {
        return new ResponseEntity<>(hazardousChemicalsService.queryNamesByType(type), HttpStatus.OK);
    }

    @GetMapping(value = "/distanceEquipmentList")
    public ResponseEntity<Object> distanceEquipmentList() {
        return new ResponseEntity<>(hazardousChemicalsService.distanceEquipmentList(), HttpStatus.OK);
    }

    @GetMapping(value = "solo")
    public ResponseEntity<Object> soloEquipment(Integer id) {
        return new ResponseEntity<>(hazardousChemicalsService.soloEquipment(id), HttpStatus.OK);
    }

    @GetMapping(value = "/distanceStandardSubstance")
    public ResponseEntity<Object> distanceStandardSubstance() {
        return new ResponseEntity<>(hazardousChemicalsService.distanceStandardSubstance(), HttpStatus.OK);
    }

    @GetMapping(value = "/distanceChemicalReagent")
    public ResponseEntity<Object> distanceChemicalReagent() {
        return new ResponseEntity<>(hazardousChemicalsService.distanceChemicalReagent(), HttpStatus.OK);
    }

    @ApiOperation("器材批量导入")
    @AnonymousAccess
    @PostMapping(value = "/equipmentBatchImport")
    public ResponseEntity<Object> equipmentBatchImport(@RequestParam MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();

        //07年的 不兼容之前
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);

        XSSFSheet sheet = xssfWorkbook.getSheetAt(0);

        //获取行数
        int lastRowNum = sheet.getLastRowNum();

        for (int i = 1; i <= lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            HazardousChemicals hazardousChemicals = new HazardousChemicals();

            if (row.getCell(0) != null) {
                hazardousChemicals.setName(row.getCell(0).getStringCellValue());
            }
            if (row.getCell(1) != null) {
                String category = null;
                switch (row.getCell(1).getStringCellValue()) {
                    case "玻璃器皿":
                        category = "1";
                        break;
                    case "实验耗材":
                        category = "2";
                        break;
                    case "采样工具":
                        category = "3";
                        break;
                    case "仪器设备":
                        category = "4";
                        break;
                }
                hazardousChemicals.setCategory(category);
            }
            if (row.getCell(2) != null) {
                hazardousChemicals.setCapacity(Convert.toFloat(row.getCell(2).getNumericCellValue()));
            }
            if (row.getCell(3) != null) {
                hazardousChemicals.setAmount(Convert.toFloat(row.getCell(3).getNumericCellValue()));
            }
            if (row.getCell(4) != null) {
                Integer unit = null;
                switch (row.getCell(4).getStringCellValue()) {
                    case "个":
                        unit = 1;
                        break;
                    case "台":
                        unit = 2;
                        break;
                    case "套":
                        unit = 3;
                        break;
                }
                hazardousChemicals.setUnit(unit);
            }
            if (row.getCell(5) != null) {
                Integer storage = null;
                switch (row.getCell(5).getStringCellValue()) {
                    case "器材室":
                        storage = 1;
                        break;
                    case "备品室":
                        storage = 2;
                        break;
                    case "外业仓库":
                        storage = 3;
                        break;
                }
                hazardousChemicals.setStorage(storage);
            }
            if (row.getCell(6) != null) {
                Integer apply = null;
                switch (row.getCell(6).getStringCellValue()) {
                    case "是":
                        apply = 1;
                        break;
                    case "否":
                        apply = 0;
                        break;
                }
                hazardousChemicals.setIsApply(apply);
            }
            if (row.getCell(7) != null) {
                Integer audit = null;
                switch (row.getCell(7).getStringCellValue()) {
                    case "是":
                        audit = 1;
                        break;
                    case "否":
                        audit = 0;
                        break;
                }
                hazardousChemicals.setIsAudit(audit);
            }
            if (row.getCell(8) != null) {
                hazardousChemicals.setLocation(row.getCell(8).getStringCellValue());
            }
            if (row.getCell(9) != null) {
                hazardousChemicals.setNote(row.getCell(9).getStringCellValue());
            }
            hazardousChemicals.setType(1);

            hazardousChemicalsService.create(hazardousChemicals, false);
        }

        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("化学试剂批量导入")
    @AnonymousAccess
    @PostMapping(value = "/chemicalBatchImport")
    public ResponseEntity<Object> chemicalBatchImport(@RequestParam MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();

        //07年的 不兼容之前
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);

        XSSFSheet sheet = xssfWorkbook.getSheetAt(0);

        //获取行数
        int lastRowNum = sheet.getLastRowNum();

        for (int i = 1; i <= lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            HazardousChemicals hazardousChemicals = new HazardousChemicals();

            if (row.getCell(0) != null) {
                hazardousChemicals.setName(row.getCell(0).getStringCellValue());
            }
            if (row.getCell(1) != null) {
                String grade = null;
                switch (row.getCell(1).getStringCellValue()) {
                    case "色谱纯":
                        grade = "1";
                        break;
                    case "优级纯":
                        grade = "2";
                        break;
                    case "分析纯":
                        grade = "3";
                        break;
                    case "化学纯":
                        grade = "4";
                        break;
                    case "基准试剂":
                        grade = "5";
                        break;
                    case "其他":
                        grade = "6";
                        break;
                }
                hazardousChemicals.setGrade(grade);
            }
            if (row.getCell(2) != null) {
                hazardousChemicals.setCapacity(Convert.toFloat(row.getCell(2).getNumericCellValue()));
            }
            if (row.getCell(3) != null) {
                Integer unit = null;
                switch (row.getCell(3).getStringCellValue()) {
                    case "L":
                        unit = 1;
                        break;
                    case "mL":
                        unit = 2;
                        break;
                    case "g":
                        unit = 3;
                        break;
                }
                hazardousChemicals.setUnit(unit);
            }
            if (row.getCell(4) != null) {
                hazardousChemicals.setAmount(Convert.toFloat(row.getCell(4).getNumericCellValue()));
            }

            if (row.getCell(5) != null) {
                Integer labId = null;
                switch (row.getCell(5).getStringCellValue()) {
                    case "化学品仓库":
                        labId = 12;
                        break;
                    case "剧毒室":
                        labId = 31;
                        break;
                    case "有机试剂":
                        labId = 32;
                        break;
                    case "样品接收室":
                        labId = 1;
                        break;
                }
                hazardousChemicals.setLabId(labId);
            }
            if (row.getCell(6) != null) {
                Integer dangerClasses = null;
                switch (row.getCell(6).getStringCellValue()) {
                    case "一般试剂（I）":
                        dangerClasses = 1;
                        break;
                    case "有机试剂（II）":
                        dangerClasses = 2;
                        break;
                    case "剧毒试剂/易致毒试剂（III）":
                        dangerClasses = 3;
                        break;
                }
                hazardousChemicals.setIsApply(dangerClasses);
            }
            if (row.getCell(7) != null) {
                Integer apply = null;
                switch (row.getCell(7).getStringCellValue()) {
                    case "是":
                        apply = 1;
                        break;
                    case "否":
                        apply = 0;
                        break;
                }
                hazardousChemicals.setIsApply(apply);
            }
            if (row.getCell(8) != null) {
                Integer audit = null;
                switch (row.getCell(8).getStringCellValue()) {
                    case "是":
                        audit = 1;
                        break;
                    case "否":
                        audit = 0;
                        break;
                }
                hazardousChemicals.setIsAudit(audit);
            }
            if (row.getCell(9) != null) {
                Timestamp timestamp = new Timestamp(row.getCell(9).getDateCellValue().getTime());
                hazardousChemicals.setExpirationDate(timestamp);
            }
            if (row.getCell(10) != null) {
                hazardousChemicals.setNote(row.getCell(10).getStringCellValue());
            }
            hazardousChemicals.setType(3);

            hazardousChemicalsService.create(hazardousChemicals, false);
        }

        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("标准物质批量导入")
    @AnonymousAccess
    @PostMapping(value = "/BZWZBatchImport")
    public ResponseEntity<Object> BZWZBatchImport(@RequestParam MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();

        //07年的 不兼容之前
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);

        XSSFSheet sheet = xssfWorkbook.getSheetAt(0);

        //获取行数
        int lastRowNum = sheet.getLastRowNum();

        for (int i = 1; i <= lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            HazardousChemicals hazardousChemicals = new HazardousChemicals();

            if (row.getCell(0) != null) {
                hazardousChemicals.setName(row.getCell(0).getStringCellValue());
            }
            if (row.getCell(1) != null) {
                hazardousChemicals.setCategory(row.getCell(1).getStringCellValue());
            }
            if (row.getCell(2) != null) {
                hazardousChemicals.setNumber(row.getCell(2).getStringCellValue());
            }
            if (row.getCell(3) != null) {
                hazardousChemicals.setBatchNumber(row.getCell(3).getStringCellValue());
            }
            if (row.getCell(4) != null) {
                hazardousChemicals.setMatrix(row.getCell(4).getStringCellValue());
            }
            if (row.getCell(5) != null) {
                Integer labId = null;
                switch (row.getCell(5).getStringCellValue()) {
                    case "化学品仓库":
                        labId = 12;
                        break;
                    case "剧毒室":
                        labId = 31;
                        break;
                    case "有机试剂":
                        labId = 32;
                        break;
                    case "样品接收室":
                        labId = 1;
                        break;
                }
                hazardousChemicals.setLabId(labId);
            }
            if (row.getCell(6) != null) {
                hazardousChemicals.setAmount(Convert.toFloat(row.getCell(6).getNumericCellValue()));
            }
            if (row.getCell(7) != null) {
                Integer unit = null;
                switch (row.getCell(7).getStringCellValue()) {
                    case "支":
                        unit = 1;
                        break;
                    case "瓶":
                        unit = 2;
                        break;
                }
                hazardousChemicals.setUnit(unit);
            }
            if (row.getCell(8) != null) {
                hazardousChemicals.setCapacity(Convert.toFloat(row.getCell(8).getNumericCellValue()));
            }
            if (row.getCell(9) != null) {
                Timestamp timestamp = new Timestamp(row.getCell(9).getDateCellValue().getTime());
                hazardousChemicals.setExpirationDate(timestamp);
            }
            if (row.getCell(10) != null) {
                hazardousChemicals.setCabinetId(Convert.toInt(row.getCell(10).getNumericCellValue()));
            }
            if (row.getCell(11) != null) {
                hazardousChemicals.setAttenuation(row.getCell(11).getStringCellValue());
            }
            if (row.getCell(12) != null) {
                hazardousChemicals.setNote(row.getCell(12).getStringCellValue());
            }
            if (row.getCell(13) != null) {
                Integer audit = null;
                switch (row.getCell(13).getStringCellValue()) {
                    case "是":
                        audit = 1;
                        break;
                    case "否":
                        audit = 0;
                        break;
                }
                hazardousChemicals.setIsAudit(audit);
            }
            hazardousChemicals.setType(2);

            hazardousChemicalsService.create(hazardousChemicals, false);
        }

        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping(value = "/fdfdfdf")
    public ResponseEntity<Object> aabb() {

        return new ResponseEntity<>(hazardousChemicalsService.aabb(), HttpStatus.OK);
    }

    @PostMapping("/warehousing")
    @Log("入库")
    @ApiOperation("入库")
    public ResponseEntity<Object> warehousing(@RequestBody Map<String, String> data) {
        int id = Convert.toInt(data.get("id"));
        HazardousChemicals hazardousChemicals = hazardousChemicalsRepository.findById(id).orElseGet(HazardousChemicals::new);

        if (hazardousChemicals.getId() == 0) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        ValidationUtil.isNull(hazardousChemicals.getId(), "HazardousChemicals", "id", id);
        HazardousChemicals newHazardousChemicals = new HazardousChemicals();
        newHazardousChemicals.copy(hazardousChemicals);
        newHazardousChemicals.setAmount(Convert.toFloat(data.get("amount")));
        if (hazardousChemicals.getType() == 1) {
            int cabinetId = -Convert.toInt(data.get("storage"));
            newHazardousChemicals.setCabinetId(cabinetId);
            newHazardousChemicals.setStorage(Convert.toInt(data.get("storage")));
        } else {
            newHazardousChemicals.setCabinetId(Convert.toInt(data.get("cabinetId")));
        }
        if (data.get("labId") != null) {
            newHazardousChemicals.setLabId(Convert.toInt(data.get("labId")));
        }
        if (data.get("expirationDate") != null) {
            String date = data.get("expirationDate").substring(0, 10) + " 00:00:00";
            newHazardousChemicals.setExpirationDate(Timestamp.valueOf(date));
        }
        if (data.get("batchNumber") != null) {
            newHazardousChemicals.setBatchNumber(data.get("batchNumber"));
        }
        hazardousChemicalsService.create(newHazardousChemicals, false);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/warehousing-v2")//给移动端使用
    @Log("入库")
    @ApiOperation("入库")
    public ResponseEntity<Object> warehousingV2(@RequestBody Map<String, String> data) {
        try {
            int id = Convert.toInt(data.get("id"));
            HazardousChemicals hazardousChemicals = hazardousChemicalsRepository.findById(id).orElseGet(HazardousChemicals::new);

            if (hazardousChemicals.getId() == 0) {
                return new ResponseEntity<>(new BaseResult(0, "试剂耗材不存在"), HttpStatus.OK);
            }
            ValidationUtil.isNull(hazardousChemicals.getId(), "HazardousChemicals", "id", id);
            HazardousChemicals newHazardousChemicals = new HazardousChemicals();
            newHazardousChemicals.copy(hazardousChemicals);
            newHazardousChemicals.setAmount(Convert.toFloat(data.get("amount")));
            if (hazardousChemicals.getType() == 1) {
                int cabinetId = -Convert.toInt(data.get("storage"));
                newHazardousChemicals.setCabinetId(cabinetId);
            } else {
                newHazardousChemicals.setCabinetId(Convert.toInt(data.get("cabinetId")));
            }
            if (data.get("labId") != null) {
                newHazardousChemicals.setLabId(Convert.toInt(data.get("labId")));
            }
            if (data.get("expirationDate") != null) {
                newHazardousChemicals.setExpirationDate(Timestamp.valueOf(data.get("expirationDate")));
            }
            if (data.get("batchNumber") != null) {
                newHazardousChemicals.setBatchNumber(data.get("batchNumber"));
            }
            hazardousChemicalsService.create(newHazardousChemicals, false);
            return new ResponseEntity<>(new BaseResult(0, "成功"), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(new BaseResult(-1, e.getMessage()), HttpStatus.OK);
        }

    }

    @PostMapping("/checkInventory")
    @Log("检查库存")
    @ApiOperation("检查库存")
    public ResponseEntity<Object> checkInventory(@RequestBody Map<String, String> map) {
        Integer id = Integer.valueOf(map.get("id").replace("h", "0"));
        Float amount = Float.valueOf(map.get("amount"));
        Map<String, String> reslt = hazardousChemicalsService.checkInventory(id, amount);
        return new ResponseEntity<>(reslt, HttpStatus.OK);
    }

    @PostMapping("/updateInventory")
    @Log("修改库存")
    @ApiOperation("修改库存")
    public ResponseEntity<Object> updateInventory(@RequestBody Map<String, String> map) {
        Integer id = Integer.valueOf(map.get("id").replace("h", "0"));
        Float amount = Float.valueOf(map.get("amount"));
        hazardousChemicalsService.updateInventory(id, amount);
        return new ResponseEntity<>("修改成功", HttpStatus.OK);
    }

    @PostMapping("/panku")
    @Log("盘库")
    @ApiOperation("盘库")
    public ResponseEntity<Object> panKu(@RequestBody List<Map<String, Object>> data) {

        long userId = SecurityUtils.getCurrentUserId();
        hazardousChemicalsService.panKu((int) userId, data);

        return new ResponseEntity<>("盘库成功", HttpStatus.OK);
    }

    @PostMapping("/panku2")
    @Log("盘库")
    @ApiOperation("盘库")
    public BaseResult panKu2(int userId, @RequestBody List<Map<String, Object>> data) {
        try {
            hazardousChemicalsService.panKu(userId, data);
        } catch (Exception e) {
            return new BaseResult(-1, "盘库失败:" + e.getMessage());
        }
        return new BaseResult(0, "盘库成功");
    }

    @GetMapping("/getByType")
    @ApiOperation("根据类型查询信息")
    public BaseResult getByType(Integer userId, Integer type) {
        List<HazardousChemicalsDto> hazardousChemicalsDtos = hazardousChemicalsService.getByType(type);

        return new BaseResult(0, "查询成功", hazardousChemicalsDtos);
    }
}
